if exists (select null from sys.procedures where name = 'sp_add_terminal_to_aggregator')
	drop procedure sp_add_terminal_to_aggregator;
go

create procedure sp_add_terminal_to_aggregator 
	@term_code varchar(12), 
	@aggr_code varchar(12), 
	@dt_beg date,
	@dt_end date,
	@params xml
as
begin
	set nocount on;

	declare @term_id int, @aggr_id int;
	select @term_id = null, @aggr_id = null
	
	select @term_id = id from terminals where code = @term_code;
	select @aggr_id = id from aggregators where code = @aggr_code;
	
	insert into aggregator_terminals(terminal_id, aggregator_id, dt_beg, dt_end, params)
	values(@term_id, @aggr_id, @dt_beg, @dt_end, @params);
end;
go

grant execute on sp_add_terminal_to_aggregator to prg;